const util = require('../../utils/util')

Page({
  data: {
    // 统计数据
    stats: {
      publishedItems: 24,
      totalSales: 5680,
      creditScore: 95
    },
    
    // 销售数据
    salesData: [
      { month: '1月', value: 3200 },
      { month: '2月', value: 2800 },
      { month: '3月', value: 4500 },
      { month: '4月', value: 5200 },
      { month: '5月', value: 4800 },
      { month: '6月', value: 5680 }
    ],
    
    // 热门商品
    hotProducts: [
      { id: 1, name: 'Java编程思想', viewCount: 128, soldCount: 25 },
      { id: 2, name: 'MacBook Pro', viewCount: 115, soldCount: 12 },
      { id: 3, name: '山地自行车', viewCount: 98, soldCount: 8 },
      { id: 4, name: '索尼耳机', viewCount: 87, soldCount: 6 },
      { id: 5, name: '考研数学资料', viewCount: 76, soldCount: 5 }
    ],
    
    // 信用分段
    creditTags: [
      { range: [90, 100], color: 'success', text: '极好' },
      { range: [80, 90], color: 'primary', text: '优秀' },
      { range: [60, 80], color: 'warning', text: '良好' },
      { range: [0, 60], color: 'danger', text: '较差' }
    ],
    
    // 进度条颜色
    progressColors: {
      success: '#67C23A',
      warning: '#E6A23C',
      danger: '#F56C6C',
      primary: '#409EFF'
    }
  },

  onLoad() {
    // 检查登录状态
    util.checkLoginStatus();
  },
  
  onShow() {
    // 更新tabBar状态 - 数据选项为索引2
    if (typeof this.getTabBar === 'function') {
      this.getTabBar().setData({
        selected: 2
      });
    }
  },
  
  // 返回首页
  goToHome() {
    wx.switchTab({
      url: '/pages/index/index'
    });
  },
  
  // 获取信用分标签
  getCreditTag() {
    const score = this.data.stats.creditScore;
    for (const tag of this.data.creditTags) {
      if (score >= tag.range[0] && score <= tag.range[1]) {
        return tag;
      }
    }
    return { color: 'primary', text: '未知' };
  },
  
  // 计算图表的最大高度
  getMaxBarHeight() {
    const values = this.data.salesData.map(item => item.value);
    return Math.max(...values);
  }
}) 